<?php

/**
 * This is the model class for table "{{apps}}".
 *
 * The followings are the available columns in table '{{apps}}':
 * @property string $app_id
 * @property string $app_name
 * @property string $app_code
 * @property string $app_logo
 * @property string $app_tag
 * @property string $app_remark
 * @property string $created_at
 * @property string $updated_at
 */
class App extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return App the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{apps}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('app_name, app_code', 'length', 'max'=>100),
			array('app_logo, app_tag, app_remark', 'length', 'max'=>255),
			array('created_at, updated_at', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('app_id, app_name, app_code, app_logo, app_tag, app_remark, created_at, updated_at', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'app_id' => 'App',
			'app_name' => 'App Name',
			'app_code' => 'App Code',
			'app_logo' => 'App Logo',
			'app_tag' => 'App Tag',
			'app_remark' => 'App Remark',
			'created_at' => 'Created At',
			'updated_at' => 'Updated At',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('app_id',$this->app_id,true);
		$criteria->compare('app_name',$this->app_name,true);
		$criteria->compare('app_code',$this->app_code,true);
		$criteria->compare('app_logo',$this->app_logo,true);
		$criteria->compare('app_tag',$this->app_tag,true);
		$criteria->compare('app_remark',$this->app_remark,true);
		$criteria->compare('created_at',$this->created_at,true);
		$criteria->compare('updated_at',$this->updated_at,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
	/**
	 * 保存前初始化数据。
	 * @see CActiveRecord#beforeSave()
	 */
	protected function beforeSave()
	{
		if($this->isNewRecord)
		{
			$this->created_at = date('Y-m-d H:i:s');
			$this->updated_at = date('Y-m-d H:i:s');
		}
		else
		{
			$this->updated_at = date('Y-m-d H:i:s');
		}
		return true;
	}
}